package com.platform.controller.platform;

import com.platform.common.annotation.Log;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.dto.device.SpeakerTypeDto;
import com.platform.system.domain.vo.device.SpeakerTypeVo;
import com.platform.system.service.ISysChatBackgroundMusicService;
import com.platform.system.service.ISysSpeakerInfoService;
import com.platform.system.service.ISysSpeakerTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/speakerInfo/speakerType")
public class SpeakerTypeController {

    @Resource
    ISysSpeakerTypeService sysSpeakerTypeService;

    @Resource
    ISysSpeakerInfoService sysSpeakerInfoService;

    @Resource
    ISysChatBackgroundMusicService sysChatBackgroundMusicService;

    /**
     * 查询发音人类类别列表
     */
    @Log(title = "查询发音人类别", businessType = BusinessType.OTHER)
    @GetMapping("/selectSpeakerTypeList")
    public TableDataInfo selectSpeakerTypeList(@Validated SpeakerTypeVo speakerTypeVo) {
        return sysSpeakerTypeService.selectSpeakerTypeList(speakerTypeVo);
    }
    /**
     * 查询发音人类别
     */
    @Log(title = "查询发音人类别", businessType = BusinessType.OTHER)
    @GetMapping("/selectSpeakerType/{uid}")
    public AjaxResult selectSpeakerType(@PathVariable(value = "uid") String uid) {
        SpeakerTypeDto speakerTypeDto = sysSpeakerTypeService.selectSpeakerType(uid);
        if(speakerTypeDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        return AjaxResult.success(speakerTypeDto);
    }


    /**
     * 新增发音人类别
     */
    @Log(title = "新增发音人类别", businessType = BusinessType.INSERT)
    @PostMapping("/insertSpeakerType")
    public AjaxResult insertSpeakerType(@RequestBody @Validated SpeakerTypeVo speakerTypeVo) {
        return sysSpeakerTypeService.insertSpeakerType(speakerTypeVo);
    }

    /**
     * 删除
     */
    @Log(title = "删除发音人类别", businessType = BusinessType.DELETE)
    @PostMapping("/deleteSpeakerType")
    public AjaxResult deleteSpeakerType(@RequestBody @Validated SpeakerTypeVo speakerTypeVo) {
        if(speakerTypeVo.getType() == 0){
            Long count = sysSpeakerInfoService.selectCountSpeakerInfoByType(speakerTypeVo.getUid());
            if(count > 0){
                throw new BusinessException(ResultEnum.DATA_EXIST_USER);
            }
        }else{
            Long count = sysChatBackgroundMusicService.selectChatBackgroundMusicCountByStyleUid(speakerTypeVo.getUid());
            if(count > 0){
                throw new BusinessException(ResultEnum.DATA_EXIST_USER);
            }
        }
        return sysSpeakerTypeService.deleteSpeakerType(speakerTypeVo);
    }

    /**
     * 修改
     */
    @Log(title = "修改发音人类别", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSpeakerType")
    public AjaxResult updateSpeakerType(@RequestBody @Validated SpeakerTypeVo speakerTypeVo) {
        return sysSpeakerTypeService.updateSpeakerType(speakerTypeVo);
    }

}
